import { Asset, JsonAsset } from "cc";
import { ITbDecode } from "../ITbDecode";
import { Tb_EquipContainer } from "./Tb_EquipContainer";
import { tb_decoder } from "../ExcelConfigDecorator";
import { Tb_Equip } from "./Tb_Equip";
import { TbContainer } from "../TbContainer";
import { GameCtx } from "../../../game/GameCtx";
import { Platform } from "../../../game/common/enums";
import { ResManager } from "../../resources/ResMgr";

// @tb_decoder(Tb_Equip)
export class Tb_EquipDecode implements ITbDecode<Tb_Equip>{
    async decode(container:TbContainer<Tb_Equip>) {
        return new Promise(async (r,j)=>{
            container.cfgs = []
            container.cfgMap.clear()
            let data:Asset =null
            if(GameCtx.ins.platform===Platform.web){//直接读本地
                data = await  ResManager.inst.loadBundleRes("resources/tb/gamelist",JsonAsset)
            
            }
            if(GameCtx.ins.platform===Platform.wechat){//读远程
    
            
            }

            //读取数据
            if(data instanceof JsonAsset){//json处理
                let list = data.json as Array<Tb_Equip>
                list.forEach(l=>{
                    container.cfgMap.set(l.id,l)
                    container.cfgs.push(l)
                })

                r(true)
                return
            }
            if(data.nativeAsset instanceof ArrayBuffer){//二进制处理
                let buffer = data.nativeAsset
                let view = new DataView(buffer)
                
                let byteLen = buffer.byteLength
                let nowIndex = 0;
                while(nowIndex<byteLen-1){
                    //数据类型
                    //1.int int[] int[][]
                    //2.string string[] string[][]
                    //3.bool
                    //4.long long[] long[][]
                    let cfg:Tb_Equip = new Tb_Equip()
                    let id = view.getUint32(nowIndex)
                    cfg.id = id;
                    nowIndex+=4
                    let start = view.getUint32(nowIndex)
                    cfg.star = start
                    nowIndex+=4
                    let level = view.getUint32(nowIndex)
                    cfg.level = level
                    nowIndex+=4
                    container.cfgs.push(cfg)
                    container.cfgMap.set(id,cfg)
                }
            }
        })

    }

}